package org.shuma.proxy.controller;


import org.shuma.proxy.commons.Result;
import org.shuma.proxy.dto.ProductDTO;
import org.shuma.proxy.entity.ProductProxy;
import org.shuma.proxy.service.ProductProxyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/proxy")
public class ProxyController {
    @Autowired
    private ProductProxyService productProxyService;

    @GetMapping("{productId}")
    public Result getProxy(@PathVariable Long productId,
                           @RequestParam(defaultValue = "0") int page,
                           @RequestParam(defaultValue = "10") int size) {
        if (productId == null) {
            return Result.error("操作失败");
        }
        PageRequest pageRequest = PageRequest.of(page, size);
        Page<ProductProxy> productProxies = productProxyService.queryByPage(productId, pageRequest);
        return Result.success(productProxies);
    }

    @DeleteMapping
    public Result delete(@RequestBody ProductProxy productProxy) {
        Long productId = productProxy.getProductId();
        String proxyIds = productProxy.getProxyIds();
        if (productId ==null || proxyIds.isEmpty() ){
            return Result.error("操作失败");
        }
        productProxyService.delete(productId, proxyIds);
        return Result.success();
    }
    @PostMapping("/appointproxy")
    public Result addProxy(@RequestBody ProductProxy productProxy) {
        Long proxyId = productProxy.getProxyId();
        Long productId = productProxy.getProductId();
        if (productId == null|| proxyId == null){
            return Result.error("操作失败");
        }
        productProxyService.addProxy(productProxy);
        return Result.success();
    }
}
